﻿using System;
using System.Collections.Generic;
using CYZHAPI.Common;
using CYZHAPI.Common.Config;
using CYZHAPI.Common.tools;
using CYZHAPI.Entity;
using CYZHAPI.IRepository.IService;
using CYZHAPI.Middlewares;
using CYZHAPI.web.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;

namespace CYZHAPI.Controllers
{
    public class uservalidateController : BaseController
    {
        private IRolService _iRolService; 
        private IHttpContextAccessor _accessor;
        public uservalidateController(IRolService objRolService, IHttpContextAccessor accessor, ILogger<loginController> logger) : base(logger)
        {
            _iRolService = objRolService;
            //_memoryCache = memoryCache;
            //_webapiOption = options.Value;
            _accessor = accessor;
        }
        
        // GET api/uservalidate
        [HttpGet("{id}")]
        public BaseResponseResult Get(int id)
        {
            BaseResponseResult response = new BaseResponseResult
            {
                code = "200",
                message = "该用户具备该权限!"
            };
            return response;
        }
        
        // POST api/login
        [HttpPost]
        public  jsonRolModel Post([FromBody]RolBean obj)
        {
            //HttpContext context = _accessor.HttpContext;
            obj.uid = obj.uid == null ? "":obj.uid;
            obj.rolid = obj.rolid.Equals("NOTROL") ? "p":obj.rolid;
            if(obj.uid.Equals("")){
                return new jsonRolModel(){
                    code = "401",
                    message = "权限认证错误！",
                    reMessage = null
                };
            }
            string[] cookieKeys = cryptionTool.DesDecrypt(obj.uid).Split('|');
            string keyUser = cryptionTool.DesDecrypt(cookieKeys[0],cookieKeys[1]).Split('|')[1];
            jsonRolModel objList = _iRolService.get(keyUser,obj.rolid);
            objList.message = "权限认证成功";
            return objList;
        }
    }
}